package net.inxweb.bluemeerkat.domain.model.glossary;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "TBL_GLS_INST")
public class GlossaryInstance implements Serializable {
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "GLS_INST_ID")
	private Long id;

	@Column(name = "GLS_INST_NM", length = 50)
	private String name;

	@ManyToOne
	@JoinColumn(name="GLS_ID")
	private GlossaryType glossaryType;
	 
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@MapKey(name="id")
	@JoinColumn(name = "ATTR_INST_ID")
	private Map<Long, AttributeInstance> attributeInstances;
	
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@MapKey(name="id")
	@JoinColumn(name = "GLS_INST_ID")
	private Map<Long, Relation> relations;

	public GlossaryInstance() {
		super();
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public GlossaryInstance(String name, GlossaryType glossaryType) {
		super();
		this.name = name;
	}

	public GlossaryType getGlossaryType() {
		return glossaryType;
	} 
	
	public Relation getRelation(String id) {
		return relations.get(id);
	}

	public void addRelation(Relation relation) {
		if (this.relations == null) {
			this.relations = new HashMap<Long, Relation>();
		}

		this.relations.put(relation.getId(), relation);
	}
	
	public void addAttributeInstance(AttributeInstance attributeInstance) {
		if (this.attributeInstances == null) {
			this.attributeInstances = new HashMap<Long, AttributeInstance>();
		} 
		this.attributeInstances.put(attributeInstance.getId(), attributeInstance); 
	}

	@Override
	public String toString() {
		return "GlossaryInstance [attributeInstances=" + attributeInstances
				+ ", glossaryType=" + glossaryType + ", id=" + id + ", name="
				+ name + ", relations=" + relations + "]";
	}  
 
}
